/*
 * 版权所有 (C) 2018 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 欢迎到知启蒙网站（https://www.zhiqim.com）购买正版软件，知启蒙还提供许多开源框架和软件。
 * 
 * 1、本软件产品所有源代码受《中华人民共和国著作权法》和其他有关法律、法规的保护，其所有知识产权归湖南知启蒙科技有限公司所有；
 * 2、禁止复制和修改。不得复制修改、翻译或改编本软件所有源代码，或者基于本软件产品创作衍生作品；
 * 3、禁止进行逆向工程。不得对本软件的源代码进行逆向工程、反编译或试图以其他方式发现软件的源代码；
 * 4、个别授权：如需进行商业性的销售、复制、分发，包括但不限于软件销售、预装、捆绑等，必须获得知启蒙的书面授权和许可；
 * 5、保留权利：本注释未明示授权的其他一切权利仍归知启蒙所有，任何人使用其他权利时必须获得知启蒙的书面同意。
 */
package net.redxyz.repost.service.task.user;

import java.util.List;

import net.redxyz.repost.service.dao.UserDao;
import net.redxyz.repost.service.dao.SysParamDao;
import net.redxyz.top.dbo.user.UserOrder;
import net.redxyz.top.dbo.user.UserOrderStat;

import org.zhiqim.kernel.logging.Log;
import org.zhiqim.kernel.logging.LogFactory;
import org.zhiqim.kernel.schedule.Task;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;

/**
 * 订购统计，一天一次
 *
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
public class UserOrderDayStatTask implements Task
{
    private static final Log log = LogFactory.getLog(UserOrderDayStatTask.class);
    
    public void execute()
    {
        String orderStatDate = null;
        String preDate = DateTimes.getPreviousDateString();

        try
        {
            //从数据库读取数据
            orderStatDate = SysParamDao.getString("timer.param", "lastOrderStatDate", "2019-01-01");
            
            if (preDate.compareTo(orderStatDate) < 0)
            {
                log.info("订单统计任务["+preDate+"]已统计");
                return;
            }
            
            //先对前一天的订购关系重作一次
            String beginTime = DateTimes.getPreviousDateString() + " 00:00:00";
            String endTime = DateTimes.getPreviousDateString() + " 23:59:59";
            UserDao.doUpdateOrder(beginTime,endTime);
            
            //再对没有统计的日期作统计
            while (preDate.compareTo(orderStatDate) >= 0)
            {
                UserOrderStat sta = new UserOrderStat();
                sta.setDayCode(orderStatDate);
                
                doOrderStat(orderStatDate, sta);
                doExpireStat(orderStatDate, sta);
                
                ORM.table().replace(sta);
                
                orderStatDate = DateTimes.getNextDateString(orderStatDate, 1);
            }
            
            //保存到数据库
            SysParamDao.doUpdate("timer.param", "lastOrderStatDate", orderStatDate);
        }
        catch (Exception e) 
        {
            log.error("订单统计任务["+orderStatDate+"]统计失败", e);
        }
    }
    
    public void doExpireStat(String dayCode, UserOrderStat sta) throws Exception
    {
        Selector selector = new Selector();
        selector.addMaybeThenGE("endTime", dayCode+" 00:00:00");
        selector.addMaybeThenLE("endTime", dayCode+" 23:59:59");
        List<UserOrder> list = ORM.table().list(UserOrder.class, selector);
        if (list.isEmpty())
        {
            log.info("订单统计任务["+dayCode+"]当日没有到期数据");
            return;
        }
        
        int orderExpNum = list.size();
        int orderExpFreeNum = 0;
        int orderExpStdNum = 0;
        int orderExpVipNum = 0;
        
        for (UserOrder order : list)
        {
            int payFee = Strings.prefixInt(order.getTotalPayFee());
            int versionNo = order.getVersionNo();
            if(payFee == 0)
                orderExpFreeNum++;
            else
            {
                if (versionNo >= 8)
                    orderExpVipNum++;
                else if (versionNo >= 2 && versionNo < 8)
                    orderExpStdNum++;
                else
                    orderExpFreeNum++;
            }
        }
        
        sta.setOrderExpNum(orderExpNum);
        sta.setOrderExpFreeNum(orderExpFreeNum);
        sta.setOrderExpStdNum(orderExpStdNum);
        sta.setOrderExpVipNum(orderExpVipNum);
        log.info("订单统计任务["+dayCode+"]当日读到到期数据成功");
    }
    
    public void doOrderStat(String dayCode, UserOrderStat sta) throws Exception
    {
        Selector selector = new Selector();
        selector.addMaybeThenGE("orderTime", dayCode+" 00:00:00");
        selector.addMaybeThenLE("orderTime", dayCode+" 23:59:59");
        selector.addOrderbyAsc("orderTime");
        List<UserOrder> list = ORM.table().list(UserOrder.class, selector);
        if (list.isEmpty())
        {
            log.info("订单统计任务["+dayCode+"]当日没有订购数据");
            return;
        }
        
        int orderTodayToatlNum = list.size();
        int orderNewNum = 0;
        int orderContinueNum = 0;
        int orderUpgradeNum = 0;
        int orderBoContinueNum = 0;
        int orderBoGiftNum = 0;

        int orderFreeNum = 0;
        int orderPayNum = 0;
        int orderPayStdNum = 0;
        int orderPayVipNum = 0;
        
        int orderPayFee = 0;
        int orderPayStdFee = 0;
        int orderPayVipFee = 0;
        
        for (UserOrder order : list)
        {
            //类型
            switch(order.getBizType())
            {
            case 1: orderNewNum++;break;
            case 2: orderContinueNum++;break;
            case 3: orderUpgradeNum++;break;
            case 4: orderBoGiftNum++;break;
            case 5: orderBoContinueNum++;break;
            }
            
            //分析免费付费
            int payFee = Integer.parseInt(order.getTotalPayFee());
            if(payFee == 0)
                orderFreeNum++;
            else
            {
                orderPayNum++;
                if(order.getVersionNo() >= 8)
                {
                    orderPayVipNum++;
                    orderPayVipFee+=Integer.parseInt(order.getTotalPayFee());
                }
                else if(order.getVersionNo() <8 && order.getVersionNo()>=2)
                {
                    orderPayStdNum++;
                    orderPayStdFee+=Integer.parseInt(order.getTotalPayFee());
                }
                else
                    orderFreeNum++;
            }
        }
            
        orderPayFee = orderPayStdFee + orderPayVipFee;
        
        sta.setDayCode(dayCode);
        sta.setOrderNum(orderTodayToatlNum);
        
        sta.setOrderNewNum(orderNewNum);
        sta.setOrderUpgradeNum(orderUpgradeNum);
        sta.setOrderContinueNum(orderContinueNum);
        sta.setOrderBoContinueNum(orderBoContinueNum);
        sta.setOrderBoGiftNum(orderBoGiftNum);
        
        sta.setOrderFreeNum(orderFreeNum);
        sta.setOrderPayNum(orderPayNum);
        
        sta.setOrderPayStdNum(orderPayStdNum);
        sta.setOrderPayVipNum(orderPayVipNum);
        
        sta.setOrderPayFee(orderPayFee);
        sta.setOrderPayStdFee(orderPayStdFee);
        sta.setOrderPayVipFee(orderPayVipFee);
        log.info("订单统计任务["+dayCode+"]当日读到订购数据成功");
    }

}
